package com.xuhuan.practice.controller;

import com.xuhuan.practice.dto.category.CategoryInfoDTO;
import com.xuhuan.practice.entity.Category;
import com.xuhuan.practice.response.Result;
import com.xuhuan.practice.service.CategoryService;
import com.xuhuan.practice.validator.group.AddGroup;
import com.xuhuan.practice.validator.group.UpdateGroup;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: huan.xu
 * @create: 2024/6/11 10:16
 * @description: 分类控制器
 **/
@RequestMapping("/category")
@RestController
public class CategoryControlller {

  @Autowired
  private CategoryService categoryService;

  /**
   * 新增分类
   *
   * @param categoryInfoDTO
   * @return
   */
  @PostMapping
  public Result add(@RequestBody @Validated(AddGroup.class) CategoryInfoDTO categoryInfoDTO) {
    categoryService.add(categoryInfoDTO);
    return Result.success();
  }

  /**
   * 文章分类列表查询
   *
   * @return
   */
  @GetMapping
  public Result<List<CategoryInfoDTO>> list() {
    List<Category> categoryList = categoryService.list();
    List<CategoryInfoDTO> categoryInfoDTOS = categoryList.stream()
        .map(category -> {
          CategoryInfoDTO categoryInfoDTO = new CategoryInfoDTO();
          BeanUtils.copyProperties(category, categoryInfoDTO);
          return categoryInfoDTO;
        })
        .collect(Collectors.toList());
    return Result.success(categoryInfoDTOS);
  }

  /**
   * 获取文章分类详情
   *
   * @return
   */
  @GetMapping("/detail")
  public Result<CategoryInfoDTO> detail(Integer categoryId) {
    Category category = categoryService.getById(categoryId);
    CategoryInfoDTO categoryInfoDTO = new CategoryInfoDTO();
    BeanUtils.copyProperties(category, categoryInfoDTO);
    return Result.success(categoryInfoDTO);
  }

  /**
   * 删除文章分类
   *
   * @param categoryId
   * @return
   */
  @DeleteMapping
  public Result delete(Integer categoryId) {
    Category category = categoryService.getById(categoryId);
    if (category == null) {
      return Result.error("文章分类ID不正确");
    }
    categoryService.deleteById(categoryId);
    return Result.success();
  }

  /**
   * 更新文章分类
   *
   * @param categoryInfoDTO
   * @return
   */
  @PutMapping
  public Result update(@RequestBody @Validated(UpdateGroup.class) CategoryInfoDTO categoryInfoDTO) {
    Category queryCategory = categoryService.getById(categoryInfoDTO.getId());
    if (queryCategory == null) {
      return Result.error("文章分类信息获取失败");
    }
    categoryService.update(queryCategory);
    return Result.success();
  }
}
